package com.linkface.idcard;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.linkface.base.LFGlobalVar;
import com.linkface.card.CardActivity;
import com.linkface.card.CardScanner;
import com.linkface.utils.LFWindowToast;

import java.util.HashMap;
import java.util.Map;

import cn.linkface.idcard.LFIDCard;
import cn.linkface.ocr.LFCard;
import cn.linkface.ocr.LFCardRecognizeListener;

/**
 * Copyright (c) 2017-2018 LINKFACE Corporation. All rights reserved.
 */
public class IDCardActivity extends CardActivity {

    /**
     * 传入身份证识别模式
     */
    public static final String EXTRA_RECOGNIZE_MODE = "com.linkface.idcard.recognizeMode";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void setRecognizeMode(int mode) {
        IDCardScanner idCardScanner = getIDCardScanner();
        if (idCardScanner != null) {
            idCardScanner.setRecognizerMode(mode);
        }
    }

    @Override
    protected void refreshRightTitleBtnView() {
        // 相册默认就是不可见
        if(mScanIsManualRecognize) {
            // 拍照识别时 相册可见
            mTvTitleRight.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 初始化IDCard扫描类
     *
     * @param context
     * @param currentFrameOrientation
     * @param isVertical
     * @return
     */
    @Override
    protected CardScanner initCardScanner(Context context, int currentFrameOrientation, boolean isVertical) {
        return new IDCardScanner(context, currentFrameOrientation);
    }

    @Override
    protected void createCardScan() {
        super.createCardScan();

        Intent intent = getIntent();
        if (intent != null) {
            int recognizeMode = intent.getIntExtra(EXTRA_RECOGNIZE_MODE, LFIDCard.FRONT);
            setRecognizeMode(recognizeMode);
        }
    }

    private IDCardScanner getIDCardScanner() {
        IDCardScanner idCardScanner = null;
        CardScanner cardScanner = getCardScanner();
        if (cardScanner instanceof IDCardScanner) {
            idCardScanner = (IDCardScanner) cardScanner;
        }
        return idCardScanner;
    }

    @Override
    protected void processManualRecognize() {
        final Bitmap confirmBmp = LFGlobalVar.sCardBitmap;

        if(confirmBmp == null || confirmBmp.isRecycled()) {
            Toast.makeText(IDCardActivity.this, "手动确认图片不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        final LFWindowToast globalToast = new LFWindowToast();
        globalToast.makeToast(IDCardActivity.this, "识别中", 3000).show();

        Map<String, Object> mExtraRequestParams = new HashMap<>();
        // 是否返回身份证类型
        mExtraRequestParams.put("return_type", "false");
        try {
            getIDCardScanner().getIDCardRecognizer().recognizeCard(0, confirmBmp, false, mExtraRequestParams, new LFCardRecognizeListener() {

                @Override
                public void onRecognizeSuccess(final Bitmap bitmap, final LFCard card) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            globalToast.hide();
                            // 将识别完后的卡信息添加到结果页
                            goToResultActivity(card, bitmap);
                        }
                    });
                }

                @Override
                public void onRecognizeFail() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            globalToast.hide();
                            LFWindowToast wToast = new LFWindowToast();
                            wToast.makeToast(IDCardActivity.this, "识别失败！", 500).show();
                        }
                    });
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
